package com.example.administrator.myapplication;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class MyContentProvider extends ContentProvider {

    private DbHelper mDbHelper;
    //主机
    public static final String AUTHORITY ="com.gc.stuprovider";
    //创建匹配Uri的工具
    static UriMatcher mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    //返回码
    public final static int MATCH_FIRST = 1;
    //先对合法Uri进行注册
    static
    {
        /*
         * 注册Uri
         * 参一：主机
         * 参二：路径
         * 参三：如果UriMatcher匹配上了这个uri，那么就返回这个匹配码
         *
         */
        mUriMatcher.addURI(AUTHORITY, "stu", MATCH_FIRST);
        //路径中的 * 表示文本  #表示数字
        mUriMatcher.addURI(AUTHORITY, "stu/#", MATCH_FIRST+1);
    }

    @Override
    public boolean onCreate() {
        // TODO: Implement this to initialize your content provider on startup.
        mDbHelper = new DbHelper(getContext());
        return true;
    }
    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        // TODO: Implement this to handle query requests from clients.
        switch (mUriMatcher.match(uri)){
            case MATCH_FIRST:
                SQLiteDatabase db = mDbHelper.getWritableDatabase();
                Cursor cursor = db.rawQuery("select * from stu",null);
                return cursor;
            case MATCH_FIRST+1:
            default:
                throw new UnsupportedOperationException("非法Uri"+uri);
        }

    }
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // Implement this to handle requests to delete one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        // TODO: Implement this to handle requests to insert a new row.
        throw new UnsupportedOperationException("Not yet implemented");
    }





    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        // TODO: Implement this to handle requests to update one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
